
// CST425 Lunch Finder
// JavaScript Functions

	var map;
	var geocoder;
	
    function load() { 
      map = new GMap2(document.getElementById("map"));
	  map.addControl(new GLargeMapControl());
	  map.addControl(new GMapTypeControl());
	  map.enableDoubleClickZoom();
      map.setCenter(new GLatLng(38.34, -98.44), 4);
      geocoder = new GClientGeocoder();
      city = document.getElementById("city");
      city.value = sGeobytesCity;
      state = document.getElementById("state");
      state.value = sGeobytesCode;      
      findStartLocation();
    }
    
    // Uses the address given by the user to geocode their
    // latitude and longitude.
    function findStartLocation() {
      var startAddress = city.value + " " + state.value;
      geocoder.getLatLng(startAddress, setStartLocation);
    }
    
    // Sets the starting latitude and longitude for the user,
    // based on the geocode response.
    function setStartLocation(point) {
      if (!point) {
        alert("Sorry, we were unable to geocode that address");
      } else {
		createCookie("lat", point.lat(), 0);
		createCookie("lng", point.lng(), 0);
      }
    }

    // Takes a JSON object containing query info and parses it,
    // geocoding each address and calling createMarker(). It then
    // adds the newly created marker to the map.
    function findLocations(locations, menuitems) {

      /*** Set up start location ***/
      var icon = new GIcon();
      icon.image = "http://www.karolwilk.com/cst425/lib/harryhead.gif";
      //icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png
      icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
      icon.iconSize = new GSize(35, 50);
      //icon.iconSize = new GSize(12, 20);
      icon.shadowSize = new GSize(22, 20);
      icon.iconAnchor = new GPoint(6, 20);
      icon.infoWindowAnchor = new GPoint(5, 1);
	  map.clearOverlays();
	  $('menu_items_output').innerHTML = "";
	  var startLat = readCookie("lat");
	  var startLng = readCookie("lng");
	  var startPoint = new GLatLng(startLat, startLng);
	  map.setCenter(startPoint, 11);
	  startMarker = new GMarker(startPoint, icon);
	  GEvent.addListener(startMarker, "click", function() {
		  //startMarker.openInfoWindowHtml('You are here!');
		  startMarker.openInfoWindowHtml('\"Hello and welcome to another fun day of CST 425!\"');
	  });
	  map.addOverlay(startMarker);
	  
	  /*** Locate and mark all restaurant locations ***/
      for (x = 0; x < locations.length; x++) {
      	var restaurant = locations[x];
      	// Construct an array of menu items
      	var index = 0;
      	var menuString = "";
	  	for (y = 0; y < menuitems.length; y++) {
	  	  if (menuitems[y].restaurantID == restaurant.id) {
	  	    menuString += menuitems[y].name + '&nbsp\;&nbsp\;&nbsp\;$' + 
	  	      menuitems[y].price + '<br>';
	  	    index++;
	  	  }
	  	}
      	var point = new GLatLng(restaurant.latitude, restaurant.longitude);
      	var dist = Math.round(startPoint.distanceFrom(point) * 0.000621);
      	var maxdist = document.getElementById("maxdist").value;
      	//if (navigator.appName.indexOf('Microsoft') != -1) maxdist = 20;
    	//if (dist <= maxdist) {
          map.addOverlay(createMarker(point, restaurant.name, restaurant.address, 
            restaurant.phone, restaurant.category, menuString, dist));    	
        //}
      }
    }
	
	function createMarker(point, n, a, p, c, m, pdist) {
        var marker = new GMarker(point);
		GEvent.addListener(marker, "click", function() {
		  marker.openInfoWindowHtml('<b>' + n + '</b>' +  ' - ' + c + '<br>' + a + 
		    '<br>' + p + '<br>' + 'Distance: ' + pdist + ' mi');
		  $('menu_items_output').innerHTML = m;
		});
		return marker;
	}

	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
